import { base_url } from './config.js';
import { getToken } from '../util/tool.js';

export const plan = {
	// 获取计划列表
	async getPlanList() {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/plan/getPlanListByJwt',
				method: 'GET',
				header: {
					'content-type': 'application/json',
					'Authorization': jwt
				},
				success(res) {
					console.log('【 Response in request getPlanList 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request getPlanList 】', err);
					reject({ success: false });
				}
			});
		});
	},
	// 计划打卡接口
	async checkinPlan(planId) {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/plan/checkinPlan',
				method: 'POST',
				header: {
					'content-type': 'application/json',
					'Authorization': jwt
				},
				data: { planId },
				success(res) {
					console.log('【 Response in request checkinPlan 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request checkinPlan 】', err);
					reject({ success: false });
				}
			});
		});
	},
	// 添加计划
	async addPlan(title, description, count) {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/plan/addPlan',
				method: 'POST',
				data: {
					title, description, count
				},
				header: {
					'content-type': 'application/json',
					'Authorization': jwt
				},
				success(res) {
					console.log('【 Response in request addPlan 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request addPlan 】', err);
					reject({ success: false });
				}
			});
		});
	},
	// 删除计划
	async deletePlan(planId) {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/plan/deletePlan',
				method: 'POST',
				data: { planId },
				header: {
					'content-type': 'application/json',
					'Authorization': jwt
				},
				success(res) {
					console.log('【 Response in request deletePlan 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request deletePlan 】', err);
					reject({ success: false });
				}
			});
		});
	},
	// 修改计划
	async updatePlan(planId, title, description, count) {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/plan/updatePlan',
				method: 'POST',
				data: {
					planId, title, description, count
				},
				header: {
					'content-type': 'application/json',
					'Authorization': jwt
				},
				success(res) {
					console.log('【 Response in request addPlan 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request addPlan 】', err);
					reject({ success: false });
				}
			});
		});
	},
	// 获取计划详情
	async getPlanInfo(planId) {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/plan/getPlanInfo',
				method: 'POST',
				header: {
					'content-type': 'application/json',
					'Authorization': jwt
				},
				data: { planId },
				success(res) {
					console.log('【 Response in request getPlanInfo 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request getPlanInfo 】', err);
					reject({ success: false });
				}
			});
		});
	},
	
};